﻿#region License

// PentaBiz - Sustainable Software Development Framework Copyright (C) 2013 Zoltán Csizmazia
// 
// This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 

#endregion


#if net20
using System;
using System.Collections.Generic;
using System.Text;
using PentaBiz;

namespace PentaBiz
{

    public delegate void Action();
    public delegate void Action<T1, T2>(T1 arg1, T2 arg2);
    public delegate void Action<T1, T2, T3>(T1 arg1, T2 arg2, T3 arg3);
    public delegate void Action<T1, T2, T3, T4>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
    public delegate TResult Func<TResult>();
    public delegate TResult Func<T, TResult>(T arg);
    public delegate TResult Func<T1, T2, TResult>(T1 arg1, T2 arg2);
    public delegate TResult Func<T1, T2, T3, TResult>(T1 arg1, T2 arg2, T3 arg3);
    public delegate TResult Func<T1, T2, T3, T4, TResult>(T1 arg1, T2 arg2, T3 arg3, T4 arg4);
}

namespace System.Linq
{

    internal static class MyExtensions
    {
        public static IEnumerable<T> Where<T>(this IEnumerable<T> collection, Func<T, bool> condition)
        {
            foreach (var item in collection)
            {
                if (condition(item))
                    yield return item;
            }
        }

        public static IEnumerable<T> OrderBy<T, T2>(this IEnumerable<T> collection, Func<T, T2> keySelector, IComparer<T2> comparer)
        {
            var list = new SortedList<T2, T>(comparer);
            foreach (var item in collection)
            {
                list.Add(keySelector(item), item);
            }

            return list.Values;

        }

        public static int Count<T>(this IEnumerable<T> collection)
        {
            int cnt = 0;
            foreach (var item in collection)
            {
                cnt++;
            }
            return cnt;
        }
        public static T[] ToArray<T>(this IEnumerable<T> collection)
        {
            var cnt = collection.Count();
            T[] r = new T[cnt];

            int i = 0;
            foreach (var item in collection)
            {
                r.SetValue(item, i);
                i++;
            }
            return r;

        }

        public static IEnumerable<T2> Select<T, T2>(this IEnumerable<T> collection, Func<T, T2> selector)
        {
            foreach (var item in collection)
            {
                yield return selector(item);
            }
        }

        public static Dictionary<TKey, TValue> ToDictionary<T, TKey, TValue>(this IEnumerable<T> collection, Func<T, TKey> keySelector, Func<T, TValue> valueSelector)
        {
            Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
            foreach (var item in collection)
            {
                dictionary.Add(keySelector(item), valueSelector(item));
            }

            return dictionary;
        }

        public static IEnumerable<T> Concat<T>(this IEnumerable<T> collection, IEnumerable<T> other)
        {
            foreach (var item in collection)
            {
                yield return item;
            }
            foreach (var item in other)
            {
                yield return item;
            }
        }
    }

}

namespace System.Collections.Generic
{
    public class HashSet<T> : List<T>
    {
        public HashSet()
            : base()
        {

        }
        public HashSet(IEnumerable<T> collection)
            : base(collection)
        {

        }
    }

}
#endif